package com.bawei.seckill.scheduled;



import com.bawei.common.constant.SeckillConstant;
import com.bawei.seckill.service.SeckillService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * 秒杀商品定时上架
 *  每天晚上3点，上架最近1天需要1天秒杀的商品
 *  当天00:00:00 - 23:59:59

 */

@Component
public class SeckillScheduled {

     @Autowired
    private SeckillService seckillService;

    @Autowired
    private RedissonClient redissonClient;

    //@Scheduled(cron = "10 *  * * * *")
    public void uploadSeckillSkuLatestDays(){

        //分布式锁防止重复上架
        RLock lock = redissonClient.getLock(SeckillConstant.upload_lock);
        //加锁
        try {
            lock.lock(10, TimeUnit.SECONDS);
            seckillService.uploadSeckillSkuLatestDays();
        } finally {
            lock.unlock();
        }

    }

}
